package com.taxi.driver.module.launch;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;

import com.qianxx.network.RetrofitRequestTool;
import com.qianxx.utils.SP;
import com.qianxx.view.dialog.ExSweetAlertDialog;
import com.taxi.driver.R;
import com.taxi.driver.common.Application;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.module.guide.GuideActivity;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.module.main.MainActivity;
import com.taxi.driver.util.RootUtil;
import com.taxi.driver.widget.dialog.ConfirmDialog;
import com.taxi.driver.widget.dialog.TwoSelectorDialog;

import javax.inject.Inject;

/**
 * 启动页
 */
public class LaunchActivity extends BaseActivity {

    @Inject
    SP mSP;

//    @Inject
//    ApiRepository mApiRepository;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        Application.getAppComponent().inject(this);

        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        if (audioManager != null) {
            int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
            int volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
            mSP.putInt("system_volume_size", 100 * volume / maxVolume);
        }
        if (RootUtil.isRoot()) {
            requestRoot();
        } else {
            requestPermission();
        }

    }

//    private void skip() {
//        //动态获取 api 地址
//        mApiRepository.getApiConfig()
//                .compose(RxUtil.applySchedulers())
//                .switchIfEmpty(Observable.error(new Throwable("no api config")))
//                .subscribe(apiEntity -> start(),
//                        ex -> start());
//    }

    private void start() {
        new Handler().postDelayed(() -> {
            /* 是否为首次使用 */
            boolean isNotFirst = mSP.getBoolean(IConstants.IS_NOT_FIRST);
            if (isNotFirst) {
                String token = RetrofitRequestTool.getToken(mSP);
                Class clz = TextUtils.isEmpty(token) ? LoginActivity.class : MainActivity.class;
                startActivity(new Intent(this, clz));
            } else {
                startActivity(new Intent(this, GuideActivity.class));
            }
            finish();
        }, 1000);
    }

    /**
     * 检测app是否已经root
     */
    private void requestEmulator() {
        new ConfirmDialog(this, "警示", "检测到您的系统是模拟器，无法运行该应用", "好的")
                .setConfirmClickListener(dialog -> {
                    finish();
                    dialog.dismiss();
                }).show();
    }

    private void requestRoot() {

        new TwoSelectorDialog(this, "警示", "您所使用手机系统已ROOT！存在数据泄露风险。请确认是否在该环境下运行APP？")
                .setCancelClickListener(dialog -> {
                    finish();
                    dialog.dismiss();
                })
                .setConfirmClickListener(dialog -> {
                    requestPermission();
                    dialog.dismiss();
                })
                .show();

    }


    private void requestPermission() {
        requestPermission(new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION,
                        Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_PHONE_STATE
                },
                this::start,
                "需要定位权限和存储权限才能正常使用应用");
    }

    @Override
    protected void onRequestPermissionFailed(String[] permissions) {
        super.onRequestPermissionFailed(permissions);
        new ExSweetAlertDialog(this, ExSweetAlertDialog.AlertDialogType.ERROR_TYPE)
                .setContentText("获取权限失败，退出应用？")
                .setConfirmText("确定")
                .show();
    }
}
